﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leo.Entities;

namespace Leo.Repository
{
    public class EmpleadoRespository : MasterRepository, IEmpleadoRespository
    {
        public List<Empleado> GetAllFromEmpleado()
        {
            var query = from p in context.Empleadoes
                        select p;
            return query.ToList();

        }

        public List<Empleado> GEtFromEmpleadoByCriterio(string criterio)
        {
            var query = from p in context.Empleadoes
                        select p;
            if (criterio!=null && criterio!=string.Empty)
            {
                query = from p in query
                        where p.DNI.ToUpper().Contains(criterio.ToUpper())
                        || p.Nombres.ToUpper().Contains(criterio.ToUpper()) ||
                        p.UserName.ToUpper().Contains(criterio.ToUpper())
                        select p;
            }
            return query.ToList();
        }

        public Empleado GetFromEmpleadoByID(int id)
        {
            throw new NotImplementedException();
        }

        public bool ExisteDuplicidadEmpleado(string dni, string userName, int id)
        {
            var query = from p in context.Empleadoes
                        where (p.DNI.ToUpper().CompareTo(dni.ToUpper()) == 0 &&
                        p.UserName.ToUpper().CompareTo(userName.ToUpper()) == 0) &&
                        p.Id != id
                        select p;
            return query.Any();
        }

        public void AddEmpleado(Empleado empleado)
        {
            context.Empleadoes.Add(empleado);
            context.SaveChanges();
        }

        public void UpdateEmpelado(Empleado empleado)
        {
            context.Entry(empleado).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveEmpelado(int id)
        {
            var existe = context.Empleadoes.Find(id);
            if (existe!=null)
            {
                context.Empleadoes.Remove(existe);
                context.SaveChanges();
            }
        }
    }
}
